import { Body, Injectable } from '@nestjs/common';
import { UsersService } from '../service/users.service';
import { JwtService } from '@nestjs/jwt';
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

@Injectable()
export class AuthService {

  constructor(
    private readonly UserService: UsersService,
    private readonly jwtService: JwtService,
  ) {}


  async validateUser(@Body() userInfo) {
    console.log(userInfo)
    const result =  prisma.userInfo.findMany({
        where:{
              uname:userInfo.uname,
              pwd:userInfo.pwd
        }
    })
      if(!result){
        return  result
      }
  }

  /**
   * @jwt认证登录
   * @param user
   */
  async login(user: any) {
    const payload = { username: user.username, sub: user.id };
    return {
      code: 200,
      data: {
        id: user.id,
        username: user.username,
      },
      access_token: this.jwtService.sign(payload),
    };
  }

  /**
   * @token验证方法
   * @param token
   */
  async verifyToken(token: string): Promise<any> {
    try {
      if (!token) return false;
      const id = this.jwtService.verify(token.replace('Bearer ', ''));
      return id;
    } catch (e) {
      return false;
    }
  }
}
